{SysSrv} = require "./SysSrv"

SupperClz = SysSrv
exports.DeptT2Srv = new Class
  Extends: SupperClz
  options:
    tab: "dept"
    sld_eny: {}
  onDraw: ->
    t = this
    o = t.options
    yield SupperClz.prototype.onDraw.apply t,arguments
    return
  hInclude: (reqOpt)->
    t = this
    o = t.options
    #获得子页面的Srv实例
    empListSrv = yield t._srvClz "DeptT2.sys.EmpListSrv||sys.EmpListSrv"
    empSrch2Whr = empListSrv.srch2Whr
    empListSrv.srch2Whr = (reqOpt,argArr,seaArr,isWhr)->
      whr = empSrch2Whr.apply empListSrv,arguments
      if o.sld_eny.id
        index = argArr.push o.sld_eny.id
        if whr.indexOf(" where ") is -1
          whr += " where d2.id=$"+index
        else
          whr += " and d2.id=$"+index
      whr
    #删除
    empDelById = empListSrv.delById
    empListSrv.delById = (reqOpt,id,tab)->
      rltSet = yield empDelById.apply this,arguments
      sql = "delete from dept_emp where emp_id=$1"
      argArr = [id]
      yield t.callSql reqOpt,sql,argArr
      rltSet
    #增加
    empAddSrv = yield t._srvClz "DeptT2.sys.EmpAddSrv||sys.EmpAddSrv"
    empSaveAddClk = empAddSrv.saveAddClk
    empAddSrv.saveAddClk = (reqOpt,entry,keyArr,returning,tab)->
      rltSet = yield empSaveAddClk.apply this,arguments
      sql = "insert into dept_emp(dept_id,emp_id) values($1,$2)"
      argArr = [o.sld_eny.id,rltSet[0].id]
      yield t.callSql reqOpt,sql,argArr
      rltSet
    return
  "@prn_trSld":{comment:"父页面选中一行时,给子页面增加筛选条件"}
  prn_trSld: (reqOpt,sld_eny)->
    t = this
    o = t.options
    o.sld_eny = sld_eny
    return
  